﻿using System;
using System.Collections.Generic;

namespace Log.Recorder
{
    /// <summary>
    /// 日志等级  All > Debug > Other > Trace > Error > Off
    /// </summary>
    enum LogLevel {
        
        /// <summary>
        ///内部日志
        /// </summary>
        RecorderLog,
        Off,
        Error,
        Trace,
        Other,
        Debug,
        All
    };
    /// <summary>
    /// 
    /// </summary>
    class LogLevelHelper
    {
        /// <summary>
        /// 
        /// </summary>
        private static Dictionary<string, LogLevel> LevelDic = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public static LogLevel GetLevel(string level)
        {
            if (string.IsNullOrEmpty(level))
            {
                return LogLevel.Off;
            }

            if (LevelDic == null) {
                LevelDic = new Dictionary<string, LogLevel>();

                Type levelType = typeof(LogLevel);
                var arr = Enum.GetValues(levelType);
                foreach (var item in arr)
                {
                    LevelDic.Add(item.ToString().ToLower(), (LogLevel) item );
                }

            }
            level = level.ToLower();
            return LevelDic.ContainsKey(level) ? LevelDic[level]: LogLevel.Off;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public static bool IsCanRecord(LogLevel level)
        {
            try
            {
                string ConfigLogLevel = LogConfig.LogLevel.ToLower();
                LogLevel ConfigLoggerLevel = GetLevel(ConfigLogLevel);
                //日志等级  All > Debug > Other > Trace > Error > Off
                return level <= ConfigLoggerLevel;
            }
            catch
            {
                return false;
            }
        }







    }
}
